﻿// -----------------------------------------------------------------------
// <copyright file="Authen.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System;
using BasicFrameworkDemo.Utility;

namespace BasicFrameworkDemo.BLL
{
    using System.Linq;

    using IBLL;
    using IDAL;
    using Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Authen : IAuthen
    {

        private readonly IRepository<LoginEntity> loginRepository;

        public Authen(IRepository<LoginEntity> loginRepository)
        {
            this.loginRepository = loginRepository;
        }

        public LoginResponse Login(LoginRequest loginRequest)
        {
            var existUser = loginRepository.EntityQueryable.Any(
                p => p.UserName == loginRequest.UserName && p.Password == loginRequest.Password);
            if(!existUser)
            {
                return new LoginResponse { Status = 0,ErrorCode = "E001"};
            }

            return new LoginResponse(); 
        }

        public RegisterResponse Register(RegisterRequest registerRequest)
        {
            var loginEntity = loginRepository.EntityQueryable.Any(p => p.UserName == registerRequest.UserName);
            if (loginEntity)
            {
                return new RegisterResponse {Status = 0,ErrorCode = "E002"};
            }

            loginRepository.Save(new LoginEntity { UserName = registerRequest.UserName, Password = registerRequest.Password });
            return new RegisterResponse();
        }

        public RemoveLoginResponse RemoveLogin(LoginEntity login)
        {
            loginRepository.Remove(login);
            return new RemoveLoginResponse();
        }
    }
}
